package cn.inbot.shopadmin.module.robot.view;

import android.app.Activity;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;

import org.jetbrains.annotations.Nullable;

import cn.inbot.shopadmin.R;
import cn.inbot.shopadmin.app.AppGlobalData;
import cn.inbot.shopadmin.app.RouterUrls;
import cn.inbot.shopadmin.module.robot.viewmodel.ScanQrcodeViewModel;
import cn.inbot.lib.framework.annotation.InjectLayout;
import cn.inbot.lib.framework.mvvm.java.PBJVMActivity;
import cn.inbot.lib.qrcode.PBQrcodeManager;
import cn.inbot.lib.qrcode.ScanCallback;
import cn.inbot.lib.util.StringUtil;
import cn.inbot.lib.widget.LoadingDialogHelper;

import static cn.inbot.lib.util.ext.ToastExtKt.showToast;

/**
 * Created by shisenkun on 2021/5/11.
 */
@Route(path = RouterUrls.DEMO_SCAN_QRCODE)
@InjectLayout(layoutResId = R.layout.activity_scan_qrcode)
public class ScanQrcodeActivity extends PBJVMActivity<ScanQrcodeViewModel> {

    private boolean isHandlingScanResult = false;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        immersiveStatusBar(R.color.scan_qrcode_status_bar, true);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.flContainer, PBQrcodeManager.INSTANCE.bindScanAbilityFragment(R.layout.fragment_scan_qrcode, new ScanCallback() {
                    @Override
                    public void onScanResult(@Nullable String result) {
                        if (!isHandlingScanResult) {
                            isHandlingScanResult = true;

                            if (StringUtil.isNotEmpty(result)) {
                                if (AppGlobalData.sLoginResult == null) {
                                    showToast("请先登陆");
                                    isHandlingScanResult = false;
                                } else {
                                    LoadingDialogHelper.INSTANCE.build(getSupportFragmentManager()).show();
                                    viewModel.scanQrcode(result);
                                }
                            } else {
                                isHandlingScanResult = false;
                                showToast("识别不到二维码");
                                finishAfterTransition();
                            }
                        }
                    }

                    @Override
                    public void onPermissionDenied() {
                        showToast("没有权限");
                        finishAfterTransition();
                    }
                })).commit();
    }

    @Override
    public void startObserve() {
        super.startObserve();
        viewModel.scanQrcodeResponseWrapper.observe(this, scanQrcodeResultResponseWrapper -> {
            isHandlingScanResult = false;
            LoadingDialogHelper.INSTANCE.hide();
            if (scanQrcodeResultResponseWrapper.getData() != null) {
                showToast("添加机器人成功");
                setResult(Activity.RESULT_OK);
                finishAfterTransition();
            } else {
                showToast("添加机器人失败,错误码:" + scanQrcodeResultResponseWrapper.getException().getErrorCode());
                setResult(Activity.RESULT_OK);
            }
        });
    }

    @Override
    public void onBackPressed() {
        finishAfterTransition();
    }
}
